package com.vessel.decoration.mvp.ui.activity.shop;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.PermissionUtil;
import com.lx.ratingbarlib.MyRatingBar;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.vessel.decoration.BuildConfig;
import com.vessel.decoration.R;
import com.vessel.decoration.app.base.MySupportActivity;
import com.vessel.decoration.app.data.entity.ShopInfoResponse;
import com.vessel.decoration.app.utils.CommonUtils;
import com.vessel.decoration.app.utils.UiHelper;
import com.vessel.decoration.di.component.DaggerShopComponent;
import com.vessel.decoration.di.module.ShopModule;
import com.vessel.decoration.mvp.contract.ShopContract;
import com.vessel.decoration.mvp.presenter.ShopPresenter;
import com.vessel.decoration.mvp.ui.adapter.ShopProductAdapter;
import com.vessel.decoration.mvp.ui.widget.RoundImage;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author Jimq
 * @date 2017/12/4
 */

public class ShopActivity extends MySupportActivity<ShopPresenter>
        implements ShopContract.View {
    @BindView(R.id.tv_title)
    TextView mTitle;
    @BindView(R.id.shop_name)
    TextView shopName;
    @BindView(R.id.shop_address)
    TextView shopAddress;
    @BindView(R.id.shop_phone)
    TextView shopPhone;
    @BindView(R.id.shop_pic)
    RoundImage shopPic;
    @BindView(R.id.score)
    MyRatingBar score;
    @BindView(R.id.product_list)
    ListView productList;
    @BindView(R.id.shoucang_iv)
    ImageView shoucang;

    private boolean shoucangStatus = false;
    private int shopId;
    private ShopProductAdapter mAdapter;
    private ShopInfoResponse shopInfo;

    private AppComponent mAppComponent;

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerShopComponent
                .builder()
                .appComponent(appComponent)
                .shopModule(new ShopModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_shop;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mAppComponent = ArmsUtils.obtainAppComponentFromContext(this);
        mTitle.setText("商家详情");
        shopId = getIntent().getIntExtra("shopId", -1);
        if (mAdapter == null) {
            mAdapter = new ShopProductAdapter();
            productList.setAdapter(mAdapter);
        }
        mPresenter.getShopDetail(shopId);
    }

    @OnClick({R.id.shop_book, R.id.shoucang_iv, R.id.iv_left})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.shop_book:
                if (shopInfo != null) {
                    if (TextUtils.isEmpty(shopInfo.getPhone())) {
                        showMessage("商家还没有留下电话，请联系管理员!");
                        return;
                    }
                    PermissionUtil.callPhone(new PermissionUtil.RequestPermission() {
                        @Override
                        public void onRequestPermissionSuccess() {
                            launchActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + shopInfo.getPhone())));
                        }

                        @Override
                        public void onRequestPermissionFailure() {
                            showMessage("权限被拒绝");
                        }
                    }, new RxPermissions(activity), mAppComponent.rxErrorHandler());
                } else {
                    showMessage("尚未获取到商家信息！");
                }
                break;
            case R.id.shoucang_iv:
                mPresenter.collect(shoucangStatus);
                break;
            case R.id.iv_left:
                killMyself();
                break;
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(String message) {
        ArmsUtils.makeText(this, message);
    }

    @Override
    public void launchActivity(Intent intent) {
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void setData2View(ShopInfoResponse shopInfoResponse) {
        this.shopInfo = shopInfoResponse;
        shopName.setText(shopInfoResponse.getShopName());
        if (shopInfoResponse.getAddress() == null || shopInfoResponse.getAddress().equals("null") || shopInfoResponse.getAddress().equals("")
                ) {
            shopAddress.setText("店铺地址：暂无");
        } else {
            shopAddress.setText("店铺地址：" + shopInfoResponse.getAddress());
        }
        if (shopInfoResponse.getPhone() == null || shopInfoResponse.getPhone().equals("null") || shopInfoResponse.getPhone().equals("")
                ) {
            shopPhone.setText("联系电话：暂无");
        } else if (CommonUtils.isMobile(shopInfoResponse.getPhone()) || CommonUtils
                .isFourMobile(shopInfoResponse.getPhone()) || CommonUtils.isPhone(shopInfoResponse.getPhone())) {
            int phoneLength = shopInfoResponse.getPhone().length();
            if (phoneLength > 4) {
                String temp = shopInfoResponse.getPhone().substring(0, phoneLength / 2 - 2) + "****" + shopInfoResponse.getPhone().substring(phoneLength / 2 + 2, shopInfoResponse.getPhone().length());
                shopPhone.setText("联系电话：" + temp);
            } else {
                shopPhone.setText("联系电话：" + shopInfoResponse.getPhone());
            }
        } else {
            shopPhone.setText("联系电话：暂无");
        }
        Glide.with(getApplicationContext()).load(BuildConfig.APP_DOMAIN + shopInfoResponse.getShopPic()).into(shopPic);
        score.setRating(shopInfoResponse.getScore());
        if (shopInfoResponse.getIsCollect() == 0) {
            shoucangStatus = true;
            shoucang.setImageResource(R.drawable.ic_shoucang);
        } else {
            shoucangStatus = false;
            shoucang.setImageResource(R.drawable.ic_shoucangdianji);
        }
        mAdapter.setList(shopInfoResponse.getProducts());
        productList.setOnItemClickListener((parent, view, position, id) -> {
            Intent intent = new Intent(ShopActivity.this, GoodsDetailActivity.class);
            intent.putExtra("info", shopInfoResponse.getProducts().get(position));
            startActivity(intent);
        });
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void updateStatus() {
        shoucangStatus = !shoucangStatus;
        if (shoucangStatus) {
            shoucang.setImageResource(R.drawable.ic_shoucang);
            UiHelper.showToast(getApplicationContext(), "取消收藏成功");
        } else {
            shoucang.setImageResource(R.drawable.ic_shoucangdianji);
            UiHelper.showToast(getApplicationContext(), "收藏成功");
        }
    }
}
